package com.example.demo01.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.demo01.R;
import com.example.demo01.adapter.MainMenuAdapter;
import com.example.demo01.consts.MainMenuData;
import com.example.demo01.entity.MainMenu;
import com.example.demo01.fragments.Found;
import com.example.demo01.fragments.Fragment01;
import com.example.demo01.fragments.Fragment02;
import com.example.demo01.fragments.Mine;
import com.example.demo01.fragments.PhoneBook;
import com.example.demo01.fragments.WeChat;
import com.example.demo01.utils.Utils;

public class FragmentDynamicDemo extends AppCompatActivity {
    Utils utils = new Utils(this);
    private GridView gv_main_bar;
    private FragmentManager fragmentManager;

    private WeChat weChat; //. 微信
    private PhoneBook phoneBook; //. 通讯录
    private Found found; //. 发现
    private Mine mine; //. 我

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_fragment_dynamic);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();
    }

    //. 初始化视图
    private void initView() {
        //. 初始化Fragment
        weChat = new WeChat();
        phoneBook = new PhoneBook();
        found = new Found();
        mine = new Mine();

        //. 初始化控件
        gv_main_bar = findViewById(R.id.gv_main_bar);

        //. 获取Fragment管理器
        fragmentManager = getSupportFragmentManager();

        fragmentManager.beginTransaction()//. 开启事务
                .add(R.id.fr_fragment_container, weChat)//. 添加Fragment01到Fragment容器中
                .commit();//. 提交事务

        addListener();
    }

    //. 添加点击事件
    private void addListener() {
        MainMenuAdapter adapter = new MainMenuAdapter(this, MainMenuData.MENUS);
        gv_main_bar.setAdapter(adapter);
        gv_main_bar.setOnItemClickListener((parent, view, position, id) -> {


            FragmentTransaction transaction = fragmentManager.beginTransaction();

            MainMenu item = adapter.getItem(position);
            switch (item.getName()) {
                case "微信":
                    transaction.replace(R.id.fr_fragment_container, weChat);
                    break;
                case "通讯录":
                    transaction.replace(R.id.fr_fragment_container, phoneBook);
                    break;
                case "发现":
                    transaction.replace(R.id.fr_fragment_container, found);
                    break;
                case "我":
                    transaction.replace(R.id.fr_fragment_container, mine);
                    break;
            }
            transaction.commit();
            adapter.setSelectedPosition(position);

        });

    }
}